package ru.xeden.core.resource;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.osgi.framework.BundleContext;


public class BundleResourceSource
    implements IResourceSource
{
    private BundleContext context;

    public BundleResourceSource(BundleContext context)
    {
        this.context = context;
    }

    @Override
    public InputStream getResourceAsStream(String resourceName)
    {
        URL resourceUrl = context.getBundle().getResource(resourceName);
        
        if (resourceUrl == null)
        {
            return null;
        }

        
        InputStream inputStream = null;
        try
        {
            
            URLConnection openConnection = resourceUrl.openConnection();
            inputStream = openConnection.getInputStream();
            return inputStream;
        }
        catch (IOException e) 
        {
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (IOException e1)
                {
                }
            }
        }
        return null;
    }
}
